"use strict";
cc._RF.push(module, 'fb0c1JzLtBMpYKcSNrQyPt3', 'UIHelper');
// resources/Script/Ssg/Helper/UIHelper.js

'use strict';

window.UIHelper = cc.Class({
    extends: cc.Component,

    properties: {},
    statics: {
        mIsHideLoadingTips: false,
        createPrefab: function createPrefab(prefab) {
            var prefabNode = null;
            if (cc.supportJit) {
                cc.supportJit = false;
                prefabNode = cc.instantiate(prefab);
                cc.supportJit = true;
            } else {
                // 如果是特殊的不支持 JIT 的平台，则不需要优化，也不能强制设置 cc.supportJit 为 true
                prefabNode = cc.instantiate(prefab);
            }
            return prefabNode;
        },

        loadRes: function loadRes(url, type, callback) {
            if (!url) {
                return;
            }
            cc.loader.loadRes(url, type, function (err, res) {
                if (err) {
                    cc.log('---------loadRes err: ' + err);
                    return;
                }
                if (callback) {
                    callback(res);
                }
            });
        },

        convertToTargetNodeSpace: function convertToTargetNodeSpace(srcNode, destNode) {
            var worldPos = srcNode.convertToWorldSpaceAR(cc.p(0, 0));
            var nodePost = destNode.parent.convertToNodeSpaceAR(worldPos);
            return nodePost;
        },

        isChinese: function isChinese() {
            var currentLanguageType = cc.sys.language;
            if (currentLanguageType == cc.sys.LANGUAGE_CHINESE) {
                return true;
            }
            return false;
        },

        //控制UI最大宽度(通过缩放UI实现)
        scaleUIMaxWidthByWin: function scaleUIMaxWidthByWin(uiNode, offsetWidth) {
            if (typeof offsetWidth == 'undefined') {
                offsetWidth = 50;
            }
            var winWidth = cc.director.getWinSize().width;
            var maxWidth = winWidth - offsetWidth;
            if (uiNode.width > maxWidth) {
                var scale = maxWidth / uiNode.width;
                uiNode.scale = scale;
            }
        },

        //处理多过长的文字内容，过长的内容用省略号代替
        longMsgUseEllipsis: function longMsgUseEllipsis(msg, maxWidth, fontSize) {
            // cc.log('-------------------longMsgUseEllipsis_msg: ' + msg)

            var msgNode = new cc.Node();
            msgNode.x = 10000;
            var msgLabel = msgNode.addComponent(cc.Label);
            msgLabel.fontSize = fontSize;
            cc.director.getScene().addChild(msgNode);
            msgLabel.string = msg;
            var curWidth = msgNode.width;
            while (curWidth > maxWidth) {
                var offsetPercent = (curWidth - maxWidth) / curWidth;
                var offsetLength = Math.ceil(msg.length * offsetPercent);
                msg = msg.substring(0, msg.length - offsetLength);
                msgLabel.string = msg;
                curWidth = msgNode.width;
            };
            msgNode.destroy();
            // return msg + '..';
            return msg;
        },

        loadNetImg: function loadNetImg(container, addres) {
            if (container == null || container == 'null') {
                return;
            }
            if (addres == null || addres == 'null') {
                return;
            }
            if (addres === "") {
                return;
            }
            if (addres.indexOf('http') == 0) {
                cc.loader.load({ url: addres, type: 'png' }, function (err, spFrame) {
                    container.spriteFrame = new cc.SpriteFrame(spFrame);
                });
            } else {
                if (SystemInfo.getPlatform() == 'qqPlay') {
                    this.loadQQNet(container, addres);
                }
            }
        },
        loadQQNet: function loadQQNet(container, addres) {
            BK.MQQ.Account.getHeadEx(addres, function (openId, imgPath) {
                var image = new Image();
                image.onload = function () {
                    var tex = new cc.Texture2D();
                    tex.initWithElement(image);
                    tex.handleLoadedTexture();
                    container.spriteFrame = new cc.SpriteFrame(tex);
                };
                image.src = imgPath;
            });
        },
        getTransScenePersistNodeJs: function getTransScenePersistNodeJs() {
            var transPersistNode = cc.director.getScene().getChildByName('TransitionSceneNode');
            if (transPersistNode) {
                return transPersistNode.getComponent('TransitionSceneNode');
            }
            return null;
        },

        getTransScenePersistNode: function getTransScenePersistNode() {
            var transPersistNode = cc.director.getScene().getChildByName('TransitionSceneNode');
            return transPersistNode;
        },

        showLoaing: function showLoaing() {
            var parent = cc.director.getScene();
            if (UIHelper.isHaveChildByName(parent, "loadingTips")) {
                return;
            }
            var self = this;
            this.addPrefabUI('Prefab/LoadingTips', parent, 100, function (loadingTips) {
                loadingTips.name = 'loadingTips';
                if (self.mIsHideLoadingTips) {
                    self.mIsHideLoadingTips = false;
                    loadingTips.destroy();
                }
            });
        },

        hideLoaing: function hideLoaing() {
            var parent = cc.director.getScene();
            var childNode = parent.getChildByName("loadingTips");
            this.mIsHideLoadingTips = true;
            if (childNode) {
                this.mIsHideLoadingTips = false;
                childNode.destroy();
            }
        },

        showTips: function showTips(tips, content, defineLabel, remainTips) {
            if (!this.getTransScenePersistNodeJs()) {
                return;
            }
            this.getTransScenePersistNodeJs().showTips(tips, content, defineLabel, remainTips);
        },

        isHaveChildByName: function isHaveChildByName(parent, childName) {
            var childNode = parent.getChildByName(childName);
            if (childNode) {
                return true;
            }
            return false;
        },

        addPrefabUI: function addPrefabUI(url, parent, zOrder, callback) {
            var self = this;
            UIHelper.loadRes(url, cc.Prefab, function (prefab) {
                var prefabUI = self.createPrefab(prefab);
                parent.addChild(prefabUI, zOrder);
                if (callback) {
                    callback(prefabUI);
                }
            });
        },

        showMsgTips: function showMsgTips(content, title, coloseLabel) {
            var parent = cc.director.getScene();
            if (UIHelper.isHaveChildByName(parent, "msgTips")) {
                return;
            }
            this.addPrefabUI('Prefab/MsgTips', parent, 100, function (loadingTips) {
                loadingTips.name = 'msgTips';
                loadingTips.getComponent("MsgTips").setMsgTipsLabel(content, title, coloseLabel);
            });
        }
    }
});

module.exports = UIHelper;

cc._RF.pop();